library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use work.FAW_TYPES.ALL;

entity TargetSampledPointsEnabler is
    Port ( i_tspe : in  COUNTER_ROWS;
           j_tspe : in  COUNTER_COLUMNS;
           clk_tspe : in  STD_LOGIC;
			  reset_tspe : in STD_LOGIC;
           en_tspe : out  TARGET_SR_ENABLE_BUS;
           en_rows_tspe : out  TARGET_SR_ENABLE_ROWS);
end TargetSampledPointsEnabler;

architecture Behavioral of TargetSampledPointsEnabler is

begin

        processo:process(clk_tspe)
        
        variable EN_INDEX : integer;
        variable EN_TSPE_VAR :   TARGET_SR_ENABLE_BUS;
		  variable EN_ROWS_TSPE_VAR :  TARGET_SR_ENABLE_ROWS;
        variable J_VAR:integer;
		  variable I_VAR:integer;
        variable WRANGE:integer;
        begin
				if (clk_tspe'event and clk_tspe='1') then
                        
               WRANGE:=N+N_DISPARITY-1;
               J_VAR:=j_tspe+2;
               --macchina a stati per le colonne (punti di campionamento) NO
               if(i_tspe>=P)then                 
						for i in 0 to RSPSRC_SR_REGISTERS-1 loop
							if(((i_tspe mod P) = 0) and (i=((i_tspe/p) mod RSPSRC_SR_REGISTERS)) and j_tspe>0)then
								for j in 0 to WRANGE-1 loop     --transitorio
									EN_TSPE_VAR(i)(j):='1';
								end loop;
							else
							--regime
								if(j_tspe=0)then
									EN_TSPE_VAR(i)(W-1):='1';       --apertura iniziale necessaria
								--	EN_TSPE_VAR(i)(W-2):='1';
								end if;
								if(J_VAR<W)then	--patch momentanea da controllare
									EN_TSPE_VAR(i)(W-1-(J_VAR-1)):='1';
								end if;
							end if;
						end loop;
											  
											  
						if((J_VAR > WRANGE )) then
							for i in 0 to RSPSRC_SR_REGISTERS-1 loop
								if((i_tspe mod P = 0) and i=((i_tspe/p) mod RSPSRC_SR_REGISTERS))then
										--SR NON a regime
									EN_TSPE_VAR(i)(WRANGE-1):='0';
								else
									EN_TSPE_VAR(i)(W-1-(J_VAR-WRANGE-1)):='0';
								end if;
							end loop;
						end if;
               end if;                 
					--macchina a stati per le righe (righe di campionamento) uguale alla reference
				
			
					I_VAR:=i_tspe+1;
					if((I_VAR mod P) = 0 and I_VAR>=P and j_tspe=W) then -- entra una riga di campionamento nella finestra
						EN_INDEX :=(I_VAR/P)mod RSPSRC_SR_REGISTERS;
						EN_ROWS_TSPE_VAR(EN_INDEX):='1';
					end if;
					
					if((I_VAR > N) and (j_tspe=W)) then
						if (((I_VAR-N)mod P) = 0) then --esce una riga di campionamento dalla finestra
							EN_INDEX :=((I_VAR-N)/P)mod RSPSRC_SR_REGISTERS;
							EN_ROWS_TSPE_VAR(EN_INDEX):='0';
						end if;
					end if;
					
					
					if(reset_tspe='1' or (i_tspe=1 and j_tspe=1) or (j_tspe=W))then                      --reset
						for i in 0 to RSPSRC_SR_REGISTERS-1 loop
							if(reset_tspe='1' or (i_tspe=1 and j_tspe=1))then
								EN_ROWS_TSPE_VAR(i):='0';
                     end if;
                     
							for j in 0 to W-1 loop
								EN_TSPE_VAR(i)(j):='0';
                     end loop;
                  end loop;
               end if;
					
					en_tspe<=EN_TSPE_VAR;
					en_rows_tspe<=EN_ROWS_TSPE_VAR;
				
				end if;
		end process;

end Behavioral;